package com.zrrd._01_servlet.servlet;

import com.zrrd._01_servlet.mapper.UserMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author WanJl
 * @version 1.0
 * @title RequestDemo03
 * @description
 * @create 2024/1/13 10:37
 */
@WebServlet("/RequestDemo07")
public class RequestDemo07 extends HttpServlet {
    UserMapper userMapper=new UserMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决Post请求的中文乱码问题
        //设置字符输入流的编码，设置的字符集要和前端页面保持一致
        req.setCharacterEncoding("UTF-8");

        //获取post请求的请求体,根据前端传递过来的请求体中的属性名获取单个属性值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        boolean result=false;
        try {
                result= userMapper.login(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置响应数据，发送给浏览器客户端(请求方)
        //响应行我们暂时不需要设置
        //设置响应头，Content-Type设置内容类型。text/html;内容类型为html网页文档。 charset=utf-8：设置网页的字符集
        resp.setHeader("Content-Type","text/html; charset=utf-8");
        //设置响应体
        PrintWriter writer = resp.getWriter();
        //判断result的值是true还是false，true就响应登录成功，否则响应登录失败
        if (result){
            writer.write("恭喜，登录成功...");
        }else{
            writer.write("恭喜，登录失败...");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
